package fhq.hcmute.modernshopping.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import fhq.hcmute.modernshopping.ProjectUtil;
import fhq.hcmute.modernshopping.R;
import fhq.hcmute.modernshopping.adapter.Product;

public class UpdatePurchaseDialog extends DialogFragment implements
		OnClickListener {
	private TextView txt_ProductName;
	private TextView txt_ProductCode;
	private TextView txt_ProductPrice;
	private TextView txt_ProductMadein;
	private Button btn_increment;
	private Button btn_decrement;
	private EditText edt_product_quanlity;
	private String title;
	private Product product;

	public UpdatePurchaseDialog(String title, Product product) {
		this.title = title;
		this.product = product;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(title)
				.setNegativeButton(getString(R.string.btn_close), this)
				.setPositiveButton(getString(R.string.btn_pick), this);
		View root = getActivity().getLayoutInflater().inflate(
				R.layout.fragment_product_info_dialog, null);
		builder.setView(root);
		txt_ProductName = (TextView) root.findViewById(R.id.txt_product_name);
		txt_ProductCode = (TextView) root.findViewById(R.id.txt_product_code);
		txt_ProductPrice = (TextView) root.findViewById(R.id.txt_product_price);
		txt_ProductMadein = (TextView) root
				.findViewById(R.id.txt_product_madein);
		btn_increment = (Button) root.findViewById(R.id.btn_increment);
		btn_increment.setOnClickListener(onIncrementOnClickListener);
		btn_decrement = (Button) root.findViewById(R.id.btn_decrement);
		btn_decrement.setOnClickListener(onDecrementOnClickListener);
		edt_product_quanlity = (EditText) root
				.findViewById(R.id.edt_product_quantity);
		initProduct(product);
		return builder.create();
	}

	private View.OnClickListener onIncrementOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			int value = Integer.parseInt(edt_product_quanlity.getText()
					.toString());
			value += 1;
			edt_product_quanlity.setText(value + "");
		}
	};

	private View.OnClickListener onDecrementOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			int value = Integer.parseInt(edt_product_quanlity.getText()
					.toString());
			value -= 1;
			edt_product_quanlity.setText(value + "");
		}
	};

	private void initProduct(Product product) {
		txt_ProductName.setText(product.getName());
		txt_ProductCode.setText(product.getCode() + "");
		txt_ProductPrice.setText(ProjectUtil.formatCurrency(product.getPrice())
				+ getString(R.string.curency));
		txt_ProductMadein.setText(product.getMadeIn());
		edt_product_quanlity.setText(product.getQuantity() + "");
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		if (which == DialogInterface.BUTTON_POSITIVE) {
			int quantity = Integer.parseInt(edt_product_quanlity.getText()
					.toString());
			product.setQuantity(quantity);
			ShoppingListProduct.getInstance().updatedProduct(product);
			ShoppingInfo.getInstance().updateView();
		}
	}
}
